<?php

	/* ----------------------------------------------------------
	 * Comm		: This class use for solr configuration 
	 * Author	: Anthonytr@n
	 * Email	: incredibletran@gmail.com
	 * Date		: Jul 30th, 2012 
	 * ----------------------------------------------------------
	 */
	
	# Call out Solr Libraries from SolrPhpClient - a Solr's API
	include 'SolrPhpClient/Apache/Solr/Service.php';
	class Data_solr {
		
		public $domain; # http://example.com:8080/solr/
	    public $host;   # example.com 
	    public $port;	# 8080
	    public $path;	# /solr
	    var $CI;
	    public function __construct(){
	        $CI	=&	get_instance();
	        #For solr's core number 1 - core name : collection1
	        $this->domain_0		=	'http://localhost:8080/solr/collection1/';
	        $this->host_0		=	'localhost';
	        $this->port_0		=	'8080';
	        $this->path_0		=	'/solr/collection1';
	        
	        #For solr's core number 2 - core name : tony1
	        $this->domain	=	'http://localhost:8080/solr/tony1/';
	        $this->host		=	'localhost';
	        $this->port		=	'8080';
	        $this->path		=	'/solr/tony1';
	        
	        #For solr's core number 3 - core name : tony2
	        $this->domain_1	=	'http://localhost:8080/solr/tony2/';
	        $this->host_1		=	'localhost';
	        $this->port_1		=	'8080';
	        $this->path_1		=	'/solr/tony2';
	        
	    }
		
		#--------------------------------------------------------------------------
		# Function add() helps insert documents into Solr index file.
		# Note: Cac truong <field> trong file schema file phu hop voi cac truong 
		# lay ra trong CSDL thi moi co the index duoc. 
		#--------------------------------------------------------------------------
		public function add($input = array()){
	    	$documents	=	array();
			$solr 		= 	new Apache_Solr_Service($this->host_1, $this->port_1, $this->path_1);
			
			if ( ! $solr->ping() ) {
				echo 'Solr service not responding.';
				exit;
			}
			$part = new Apache_Solr_Document();
			foreach ( $input as $key => $value ) {
				if ( is_array( $value ) ) {
					foreach ( $value as $data ) {
						$part->setMultiValue( $key, $data );
					}
				}else {
					$part->$key = $value;
				}
			}
			
			$documents[] = $part;
			
			# Load the documents into the index
			try {
				$solr->addDocuments( $documents );
				$solr->commit();
				$solr->optimize();
			}
			catch ( Exception $e ) {
				echo $e->getMessage();
			}
		}# End function add().
		
		
		#--------------------------------------------------------------------------
		# Function search() helps to show all results match input keyword.
		#--------------------------------------------------------------------------
		public function search($keyword = array(), $offset = 0, $limit = 5) {
			/* When u want to search on different cores you just change 3 parameters bellow
			 * For instance to core 1: $solr = new Apache_Solr_Service($this->host0, $this->port0, $this->path0)
			*/
				
			$solr = new Apache_Solr_Service($this->host_1, $this->port_1, $this->path_1);
		
			if ( ! $solr->ping() ) {
				echo 'Solr service not responding.';
				exit;
			}
			# Set Solr's parameters for searching - Here we set for core number 2
			$additionalParameters = array(
											'sort'			=> 'score desc',
											'hl' 			=> 'true',					// For showing highlighting functionality
											'hl.useFastVectorHighlighter' => 'true',
											'hl.fl' 		=> 'text',				// Specifes which field we want to highlight.
											'hl.simple.pre' => '<b>',					// Changing the tag surround the
											'hl.simple.post'=> '</b>',					// highlighting text
											'mlt'			=> 'true',					// This parameter says that we want to put more like this (mlt) component to the result processing
											'mlt.fl'		=> 'text',				// Specifies which field we want to use with the more like this component.
											'mlt.mintf'		=> 1,						// Tells Solr to ignore terms from source document with term frequency below the given value.
											'mlt.mindf'		=> 1,						// Tells Solr the words that appear in less than the value of the parameter documents should be ignored.
										 );
			//if($filter != 0) $additionalParameters['fq'] = "cate_id:$filter";			//	For Filtering query - Here by category of question
			$rows	=	array();
			foreach ( $keyword as $query ) {
				$response = $solr->search( $query, $offset, $limit, $additionalParameters );
					
				if ( $response->getHttpStatus() == 200 ) {
					if ( $response->response->numFound > 0 ) {
						$total_num 			=  	$response->response->numFound;
						$rows['total_num']	=	$total_num;
						foreach ($response->highlighting as $k => $v){
							$rows[$k] =	$v->text;
						}
					}
				}else{
					echo $response->getHttpStatusMessage();
				}// End - if()
			}// End - foreach
			if($rows){
				return $rows;
			}
		}# End search()
		
		
		/* public function search($keyword = array(), $filter, $offset = 0, $limit = 5) {
			 # When u want to search on different cores you just change 3 parameters bellow
			 # For instance to core 1: $solr = new Apache_Solr_Service($this->host0, $this->port0, $this->path0)
			 
			
			$solr = new Apache_Solr_Service($this->host, $this->port, $this->path);
				
			if ( ! $solr->ping() ) {
				echo 'Solr service not responding.';
				exit;
			}
			# Set Solr's parameters for searching - Here we set for core number 2
			$additionalParameters = array(
											'sort'			=> 'score desc',
											'hl' 			=> 'true',					// For showing highlighting functionality
											'hl.useFastVectorHighlighter' => 'true',
											'hl.fl' 		=> 'detail',				// Specifes which field we want to highlight.
											'hl.simple.pre' => '<b>',					// Changing the tag surround the 
											'hl.simple.post'=> '</b>',					// highlighting text
											'mlt'			=> 'true',					// This parameter says that we want to put more like this (mlt) component to the result processing
											'mlt.fl'		=> 'detail',				// Specifies which field we want to use with the more like this component.
											'mlt.mintf'		=> 1,						// Tells Solr to ignore terms from source document with term frequency below the given value.
											'mlt.mindf'		=> 1,						// Tells Solr the words that appear in less than the value of the parameter documents should be ignored.   
										 );
			if($filter != 0) $additionalParameters['fq'] = "cate_id:$filter";			//	For Filtering query - Here by category of question
			$rows	=	array();
			foreach ( $keyword as $query ) {
				$response = $solr->search( $query, $offset, $limit, $additionalParameters );
					
				if ( $response->getHttpStatus() == 200 ) {
					if ( $response->response->numFound > 0 ) {
						$total_num 			=  	$response->response->numFound;
						$rows['total_num']	=	$total_num;
						foreach ($response->highlighting as $k => $v){
							$rows[$k] =	$v->detail;
						}
					}
				}else{
					echo $response->getHttpStatusMessage();
				}
			}
			if($rows){
				return $rows;
			}
		}# End search() */

		
		#---------------------------------------------------------------------------------------
		# Function remove() helps to delete the document with specified ID from Solr index file.
		#---------------------------------------------------------------------------------------
		public function remove($id){
			$solr = new Apache_Solr_Service($this->host, $this->port, $this->path);
			
			if ($solr->ping() ) {
				$solr->deleteById($id);
				$solr->commit();
			}else{
				echo 'Solr service not responding.';
				exit;
			}
		}# End remove()
		
		
		
		
	}# End Class Solr
	
?>	